<?php
/**
 * Модель user
 * 
 * Класс модели
 * @author Anton Zavidov
 * @package myLiveShop
 */

/**
 * Модель user
 *
 */
class ModelUser extends Model {
	
	const TABLE				= 'user';
	const F_USER_ID  		= 'user_id';
	const F_GROUP_ID  		= 'group_id';
	const F_LANG_ID  		= 'lang_id';
	const F_COUNTRY_ID   	= 'country_id';
	const F_CITY_ID   		= 'city_id';
	const F_IMAGE_ID  		= 'image_id';
	const F_PASSWORD  		= 'password';
	const F_EMAIL  			= 'email';
	const F_CHECKED_EMAIL   = 'checked_email';
	const F_BANNED  		= 'banned';
	const F_MONEY   		= 'money';
	const F_REPUTATION   	= 'reputation';
	const F_CITY  	 		= 'city';
	const F_ADDRESS			= 'address';
	const F_SEX				= 'sex';
	const F_REGISTRATION_DATE= 'registration_date';
	const F_BIRTHDAY		= 'birthday';
	const F_SITE 			= 'site';
	const F_TEL				= 'tel';
	const F_ICQ				= 'icq';
	const F_NAME			= 'name';
	const F_ABOUT			= 'about';
	
	public function __construct($className=null){
		parent::__construct(__CLASS__);
		$this->name='user';
		$this->id_field=array(self::F_USER_ID);
		$this->fields=array(
			self::TABLE.DOT.self::F_USER_ID 		=> new Field($this,self::F_USER_ID,self::TABLE,array(
										'lenght'=>11,
										'important'=>true,
										'auto'=>true)),
			self::TABLE.DOT.self::F_GROUP_ID 		=> new Field($this,self::F_GROUP_ID,self::TABLE,array(
										'lenght'=>2,
										'auto'=>true,
										'join_table'=>'group')),
			self::TABLE.DOT.self::F_LANG_ID 		=> new Field($this,self::F_LANG_ID,self::TABLE,array(
										'lenght'=>2,
										'auto'=>true,
										'join_table'=>'lang')),
			self::TABLE.DOT.self::F_COUNTRY_ID 		=> new Field($this,self::F_COUNTRY_ID,self::TABLE,array(
										'lenght'=>5,
										'auto'=>true,
										'join_field'=>'region'.DOT.'region_id')),
			self::TABLE.DOT.self::F_CITY_ID 		=> new Field($this,self::F_CITY_ID,self::TABLE,array(
										'lenght'=>5,
										'auto'=>true,
										'join_field'=>'region'.DOT.'region_id')),
			self::TABLE.DOT.self::F_IMAGE_ID 		=> new Field($this,self::F_IMAGE_ID,self::TABLE,array(
										'lenght'=>5,
										'auto'=>true,
										'join_table'=>'image')),
			self::TABLE.DOT.self::F_PASSWORD 		=> new Field($this,self::F_PASSWORD,self::TABLE,array(
										'type'=>Field::TYPE_STRING,
										'lenght'=>32/*,
										'important'=>true*/)),
			self::TABLE.DOT.self::F_EMAIL 			=> new Field($this,self::F_EMAIL,self::TABLE,array(
										'type'=>Field::TYPE_STRING,
										'validate_type'=>Validator::VALIDATE_EMAIL,
										'lenght'=>50,
										'important'=>true)),
			self::TABLE.DOT.self::F_CHECKED_EMAIL 	=> new Field($this,self::F_CHECKED_EMAIL,self::TABLE,array(
										'type'=>Field::TYPE_STRING,
										'validate_type'=>Validator::VALIDATE_HASH,
										'lenght'=>16)),
			self::TABLE.DOT.self::F_BANNED 			=> new Field($this,self::F_BANNED,self::TABLE,array(
										'validate_type'=>Validator::VALIDATE_BOOL)),
			self::TABLE.DOT.self::F_MONEY 			=> new Field($this,self::F_MONEY,self::TABLE,array(
										'lenght'=>6)),
			self::TABLE.DOT.self::F_REPUTATION 		=> new Field($this,self::F_REPUTATION,self::TABLE,array(
										'lenght'=>4)),
			self::TABLE.DOT.self::F_CITY 			=> new Field($this,self::F_CITY,self::TABLE,array(
										'type'=>Field::TYPE_STRING,
										'lenght'=>50)),
			self::TABLE.DOT.self::F_ADDRESS 		=> new Field($this,self::F_ADDRESS,self::TABLE,array(
										'type'=>Field::TYPE_STRING,
										'lenght'=>150)),
			self::TABLE.DOT.self::F_SEX 			=> new Field($this,self::F_SEX,self::TABLE,array(
										'validate_type'=>Validator::VALIDATE_BOOL)),
			self::TABLE.DOT.self::F_REGISTRATION_DATE=>new Field($this,self::F_REGISTRATION_DATE,self::TABLE,array(
										'type'=>Field::TYPE_DATE)),
			self::TABLE.DOT.self::F_BIRTHDAY		=> new Field($this,self::F_BIRTHDAY,self::TABLE,array(
										'type'=>Field::TYPE_DATE)),
			self::TABLE.DOT.self::F_SITE 			=> new Field($this,self::F_SITE,self::TABLE,array(
										'type'=>Field::TYPE_STRING,
										'lenght'=>100)),
			self::TABLE.DOT.self::F_TEL 			=> new Field($this,self::F_TEL,self::TABLE,array(
										'type'=>Field::TYPE_STRING,
										'lenght'=>20)),
			self::TABLE.DOT.self::F_ICQ 			=> new Field($this,self::F_ICQ,self::TABLE,array(
										'lenght'=>11)),
			self::TABLE.DOT.self::F_NAME 			=> new Field($this,self::F_NAME,self::TABLE,array(
										'type'=>Field::TYPE_STRING,
										'lenght'=>150)),
			self::TABLE.DOT.self::F_ABOUT 			=> new Field($this,self::F_ABOUT,self::TABLE,array(
										'type'=>Field::TYPE_TEXT))
		);
	}
	
	public function login($email,$password){
		import('model.image');
		import('model.lang');
		import('model.group');
		$user=$this->loadOne(false,array(
			ModelUser::F_USER_ID,
			ModelUser::F_CHECKED_EMAIL,
			ModelUser::F_NAME,
			ModelUser::F_LANG_ID,
			ModelUser::F_BANNED,
			ModelUser::F_GROUP_ID,
			ModelImage::TABLE.DOT.ModelImage::F_FILE,
			ModelLang::TABLE.DOT.ModelLang::F_SHORT,
			ModelGroup::TABLE.DOT.ModelGroup::F_TITLE,
			ModelGroup::TABLE.DOT.ModelGroup::F_RIGHTS
			),array(
				'join'=>array(
					array('table'=>ModelLang::TABLE, 'key'=>ModelLang::F_LANG_ID, 'join_table'=>ModelUser::TABLE, 'join_field'=>ModelUser::F_LANG_ID),
					array('table'=>ModelImage::TABLE, 'key'=>ModelImage::F_IMAGE_ID, 'join_table'=>ModelUser::TABLE, 'join_field'=>ModelUser::F_IMAGE_ID),
					array('table'=>ModelGroup::TABLE, 'key'=>ModelGroup::F_GROUP_ID, 'join_table'=>ModelUser::TABLE, 'join_field'=>ModelUser::F_GROUP_ID)
				),
				'where'=>ModelUser::F_EMAIL.'="'.$email.'" AND '.ModelUser::F_PASSWORD.'="'.$password.'"'
			)
		);
		return $user;
	}
	/**
	 * получаем имя пользователя или список имён
	 *
	 * @param mixed $id число или масив
	 * @return mixed строка или массив
	 */
	public function getName($id){
	    if(is_numeric($id)){
	       $user=$this->loadOne($id,array(ModelUser::F_NAME));
	    }elseif(is_array($id)){
	       $user=$this->loadOne(false,array(ModelUser::F_NAME),array(
				'where'=>ModelUser::F_USER_ID .' IN('.implode(', ',$id).')'
			 )
		   );
	    }else{
	        return false;
	    }
	    return $user;
	}
    public function checkEmail($email){
        if (!$email) return false;
        $user = $this->loadOne(false, array(ModelUser::F_USER_ID), array(
            'where'=>ModelUser::F_EMAIL.'="'.$email.'"',
            'limit'=>1
        ));
        if(isset($user[ModelUser::F_USER_ID]))return false;
        return true;
    }
}